﻿namespace EyeOpen.BuildLab.Build
{
	using System.Collections.Generic;
	using System.IO;

	using EyeOpen.Build;
	using EyeOpen.Dependency;
	using EyeOpen.Io;

	using Microsoft.Build.Utilities;

	public class CustomWorkFlowTask : Task
	{
		private readonly IDependencyResolver dependencyResolver;
		private readonly bool testing = true;

		private string solutionFilePath;
		private string newSolutionFilePath;


		public CustomWorkFlowTask()
			: this(new BuildDependencyRegister())
		{
			testing = false;
		}

		public CustomWorkFlowTask(IDependencyRegister dependencyRegister)
		{
			this.dependencyResolver = 
				new DependencyResolver(dependencyRegister);
		}

		public string OutDir { get; set; }

		public string SolutionDir { get; set; }

		public override bool Execute()
		{
			Configure();

			var taskList = new List<EyeOpen.Build.ITask>();

			////var removeProjectsFromSolutionToBuildBuildTask = ConfigureRemoveProjectListTask();
			var msBuildTask = this.ConfigureMsBuildTask();
			var copyBuildTask = this.ConfigureCopyBuildTask();

			////taskList.Add(removeProjectsFromSolutionToBuildBuildTask);
			taskList.Add(msBuildTask);
			taskList.Add(copyBuildTask);

			foreach (var task in taskList)
			{
				ConfigureTask(task);

				if (!task.Execute())
				{
					return false;
				}
			}

			return true;
		}

		private ICopyTask ConfigureCopyBuildTask()
		{
			var copyBuildTask =
				this.dependencyResolver.Resolve<ICopyTask>().Configure(
					new CopyTaskConfigurer(
						Path.Combine(this.SolutionDir, @"Main\Configurations\Mvc.Web.config"),
						Path.Combine(this.OutDir, @"_PublishedWebsites\EyeOpen.Mvc\Web.config")));

			return copyBuildTask;
		}

		private IMsBuildTask ConfigureMsBuildTask()
		{
			var msBuildTask =
				this.dependencyResolver.Resolve<IMsBuildTask>().Configure(
					new MsBuilTaskConfigurer(this.newSolutionFilePath, this.OutDir, new[] { "Clean", "Build" }));

			return msBuildTask;
		}

		private IRemoveProjectsFromSolutionFileTask ConfigureRemoveProjectListTask()
		{
			var removeProjectList = new[]
				{
					@"Io\EyeOpen.Io.csproj", @"Build\EyeOpen.Build.csproj", @"Build.Contracts\EyeOpen.Build.Contracts.csproj",
					@"BuildLab\EyeOpen.BuildLab.Build.csproj", @"Build.Test\EyeOpen.Build.Test.csproj",
					@"Dependency\EyeOpen.Dependency.csproj"
				};

			var removeProjectListTask =
				this.dependencyResolver.Resolve<IRemoveProjectsFromSolutionFileTask>().Configure(
					new RemoveProjectsFromSolutionFileTaskConfigurer(this.solutionFilePath, this.newSolutionFilePath, removeProjectList));

			return removeProjectListTask;
		}

		private void Configure()
		{
			solutionFilePath = Path.Combine(SolutionDir, @"Main\EyeOpen.BuildTest.sln");

			var solutionFile =
				dependencyResolver
					.Resolve<IFile>()
					.GetFileInfo(solutionFilePath);

			var solutionFileName = solutionFile.Name.Replace(solutionFile.Extension, null) + ".Changed";

			newSolutionFilePath = Path.Combine(solutionFile.DirectoryName, solutionFileName + solutionFile.Extension);
		}

		private void ConfigureTask(ITask task)
		{
			if (testing)
			{
				return;
			}

			var taskType = task.GetType();
			taskType.GetProperty("BuildEngine").SetValue(task, this.BuildEngine, null);
			taskType.GetProperty("HostObject").SetValue(task, this.HostObject, null);
		}
	}
}
